<template>
  <header class="m-header">
    <!-- Logo -->
    <div class="logo">
      <a href="/">
        <img src="../assets/logo/png/logo-no-background.png" alt="Logo" width="120">
      </a>
    </div>

    <!-- navagator -->
    <nav class="main-nav">
      <ul>
        <li v-for="menuRoute in menuRoutes" :key="menuRoute.path">
          <a @click="router.push(menuRoute.path)">{{ menuRoute.meta.title }}</a>
        </li>
      </ul>
    </nav>

    <!-- funtional -->
    <div class="header-right">
      <div class="search-container">
        <el-input>
          <template #append>
            <el-button><i class="ri-search-line"></i></el-button>
          </template>
        </el-input>
      </div>
      <!-- 登录状态 -->
      <div class="login-status">
        <div v-if="!isLogin" class="login-button" @click="handleLogin">
          <i class="ri-login-box-line"></i>
        </div>
        <el-dropdown v-else trigger="click" @command="handleCommand">
          <el-button circle size="small">
            <el-avatar :size="40" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="info">
                <i class="ri-user-location-line"></i>个人信息
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <i class="ri-logout-box-line"></i>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { storeToRefs } from 'pinia';
import useLoginState from '@/stores/userinfo';

const router = useRouter();
const route = useRoute();
const loginState = useLoginState();
const menuRoutes = ref([])

const { isLogin } = storeToRefs(loginState);
// 初始化菜单路由
const updateMenuRoutes = function() {
  const routes = router.options.routes[0].children;
  menuRoutes.value = routes.filter(route => 
    (route.meta?.menu && !route.meta?.requiresAuth) ||
    (route.meta?.menu && route.meta?.requiresAuth && loginState.isLogin)
  );
  if (loginState.isLogin) {
    router.options.routes.forEach(route => {
      if (route.meta?.menu && route.meta?.requiresAuth) {
        menuRoutes.value.push(route);
      }
    });
  }
}

onMounted(() => {
  updateMenuRoutes();
})

const handleLogin = function() {
  router.push({path: '/login'});
}

const handleSearch = function() {
}

const handleCommand = function(command) {
  switch (command) {
    case 'info':
      router.push({path: '/userinfo'});
      break;
    case 'logout':
      loginState.doLogout();
      if (route.meta.requiresAuth) {
        router.push({path: '/'});
      } else {
        router.replace({path: route.path});
      }
      break;
    default:
      console.error('command not found: ', command);
      break;
  }
}

const subscribe = loginState.$subscribe((mutation, state) => {
  updateMenuRoutes();
})
</script>

<style scoped>
.m-header {
  background-color: #333;
  color: white;
  text-align: center;

  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 5%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.logo img {
  height: 30px;
}

.main-nav ul {
  display: flex;
  gap: 2rem;
  list-style: none;
}

.main-nav a {
  text-decoration: none;
  color: #ddd;
  font-weight: bold;
  font-size: 18px;
  transition: color 0.3s;
  cursor: pointer;
}

.main-nav a:hover {
  color: #007bff;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.header-right .search-container {
  display: flex;
  align-items: center;
  max-width: 500px;
  margin: 20px auto;
}

.header-right .search-box {
  flex: 1;
  padding: 12px 45px 12px 16px;
  border: 2px solid #007bff;
  border-radius: 25px; /* 圆角设置[2,4](@ref) */
  font-size: 16px;
  outline: none;
  transition: all 0.3s;
}

.header-right .search-box:focus {
  border-color: #0056b3;
  box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}

.header-right .search-container input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}

.search-button {
  position: relative;
  right: 40px;
  padding: 8px 20px;
  background: #197278;
  border: none;
  border-radius: 20px; /* 按钮圆角[2](@ref) */
  color: white;
  cursor: pointer;
  transition: background 0.3s;
}

.search-button:hover {
  background: #283d3b;
}

.search-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  background: url('search-icon.png') no-repeat center/cover; /* 图标引入[1,4](@ref) */
}
.login-button {
  padding: 12px;
  background: #197278;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.login-button:hover {
  background: #406562;
  transform: scale(1.1);
}

.login-button .v-icon {
  font-size: 24px;
}

/* .header-right ::v-deep .el-input-group__append .el-button {
  background-color: #197278 !important;
  border-color: #197278 !important;
}

.header-right ::v-deep .el-input-group__append .el-button:hover {
  background-color: #12565b !important;
}

.header-right ::v-deep .el-input-group__append .el-button:active {
  background-color: #12565b !important;
} */
</style>